package com.yichen.rent.service;

import com.yichen.rent.mapper.CountMapper;
import com.yichen.rent.response.CityHouseResponse;
import com.yichen.rent.utils.AreaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticService {

    @Autowired
    private CountMapper statisticMapper;

    @Autowired
    private AreaUtil areaUtil;

    public List<CityHouseResponse> cityHouseCount() {
        return statisticMapper.countCityRentHouse();
    }

    public List<CityHouseResponse> cityNewHouseCount() {
        return statisticMapper.countCityNewHouse();
    }

    public List<CityHouseResponse> cityOldHouseCount() {
        return statisticMapper.countCityOldHouse();
    }

    /**
     * 可租房源城市分布
     * @return
     */
    public List<Map<String,Object>> locationMapCountRent() {
        List<Map<String,Object>> list = new ArrayList<>();
        List<CityHouseResponse> cityHouseResponses = cityHouseCount();
        for (CityHouseResponse cityHouseResponse : cityHouseResponses) {
            Map<String,Object> map = new HashMap<>();
            String city = cityHouseResponse.getCity();
            Map<String, String> center = areaUtil.cityInfo(city);
            map.put("center",center);
            map.put("address",city);
            map.put("count","共"+cityHouseResponse.getCount());
            map.put("active",false);
            list.add(map);
        }
        return list;
    }

    /**
     * 可租房源地区分布
     * @return
     */
    public List<Map<String,Object>> locationMapCountRentCounty() {
        List<Map<String,Object>> list = new ArrayList<>();
        List<CityHouseResponse> cityHouseResponses = statisticMapper.countCountyRentHouse();
        for (CityHouseResponse cityHouseResponse : cityHouseResponses) {
            Map<String,Object> map = new HashMap<>();
            String city = cityHouseResponse.getCity();
            Map<String, String> center = areaUtil.cityInfo(city);
            map.put("center",center);
            map.put("address",city);
            map.put("count",cityHouseResponse.getCount());
            map.put("active",false);
            list.add(map);
        }
        return list;
    }

    /**
     * 二手房源地区分布
     * @return
     */
    public List<Map<String,Object>> locationMapCountOldCounty() {
        List<Map<String,Object>> list = new ArrayList<>();
        List<CityHouseResponse> cityHouseResponses = statisticMapper.countCountyOldHouse();
        for (CityHouseResponse cityHouseResponse : cityHouseResponses) {
            Map<String,Object> map = new HashMap<>();
            String city = cityHouseResponse.getCity();
            Map<String, String> center = areaUtil.cityInfo(city);
            map.put("center",center);
            map.put("address",city);
            map.put("count",cityHouseResponse.getCount());
            map.put("active",false);
            list.add(map);
        }
        return list;
    }

    /**
     * 楼盘房源地区分布
     * @return
     */
    public List<Map<String,Object>> locationMapCountNewCounty() {
        List<Map<String,Object>> list = new ArrayList<>();
        List<CityHouseResponse> cityHouseResponses = statisticMapper.countCountyNewHouse();
        for (CityHouseResponse cityHouseResponse : cityHouseResponses) {
            Map<String,Object> map = new HashMap<>();
            String city = cityHouseResponse.getCity();
            Map<String, String> center = areaUtil.cityInfo(city);
            map.put("center",center);
            map.put("address",city);
            map.put("count",cityHouseResponse.getCount());
            map.put("active",false);
            list.add(map);
        }
        return list;
    }
}
